import json
from copy import deepcopy

from re_common.baselibrary.utils.core.mdeprecated import try_except2_async, try_except2

from apps.crawler_platform.core_callback import *
from apps.crawler_platform.core_callback.err_callback import all_client_back_callback
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from apps.crawler_platform.core_platform.g_model import EtlDealModel, \
    EtlCallBackModel, ModelColl, DownModel
from apps.crawler_platform.etl_platform.base_etl_step import BaseETLStep, BaseEtlItems
from apps.crawler_platform.etl_platform.etl_parse import EtlItems

# 不执行逻辑，目的是为了不让 from apps.crawler_platform.core_callback import *
# 被编辑器认为没有使用，快捷清除时就不会被清除
from apps.crawler_platform.util.nosqlhelper import NOSQLHelper
from apps.sql_app.mmongodb import Coll

STATICA_HACK = False
# globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:  # pragma: no cover
    null()



class EtlParseRef(BaseETLStep):

    def __init__(self, request, inputs):
        super().__init__()
        self.err_tag = "etl_parse_ref"
        self.pm = EtlItems()

        self.input = inputs
        self.pm.task_name = inputs.data.task_name
        self.pm.task_tag = inputs.data.task_tag
        self.pm.id_dicts = inputs.data.id_dicts
        self.pm.field = {"last_data": 1}
        self.pm.Id = "_".join([v for k, v in self.pm.id_dicts.items()])

        self.pm.redis = request.app.state.redis

        self.request = request
        self.step_info = ""
        # 解析字段字典
        self.para_dicts = {}

        self.sql_model_class = None
        self.html_model_class = None

    def set_model(self):
        self.sql_model_class = ModelColl.get_sql_model(self.pm.task_setting.sql_model_name)
        self.html_model_class = ModelColl.get_html_model(self.pm.task_setting.model_name)
        self.pm.down_model = DownModel[self.html_model_class](down_dict={})
        self.pm.down_model_x = self.html_model_class


    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def get_sql(self):
        self.set_model()
        tmpdic = deepcopy(self.pm.id_dicts)
        tmpdic.pop("keyid")
        row = await CoreSqlMixin.get_sql_etl_parse(
            self.pm.task_setting.table_name,
            self.pm.task_setting.table_index,
            self.pm.task_name,
            self.pm.task_tag,
            tmpdic)
        if not row:
            return False, {
                "err_msg": "qk article local etl sql查询不存在;" + ",".join([self.pm.task_tag, str(self.pm.id_dicts)])}
        self.pm.sql_model = self.sql_model_class.parse_obj(row)
        return True, row

    @try_except2()
    def verify_para(self):
        """
        验证解析出来的数据
        """
        return True, {}

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def save_sql(self, code, err_msg=""):
        """
        与关系型sql的处理 包括存储下一级的sql以及本级sql的状态和字段更新
        """

        if code is False:
            # 如果前面的布尔值是fasle，就直接写7错误
            code = 7
            err_msg = str(err_msg)
        else:
            if self.pm.deal_model.status == "FAILED":
                code = self.pm.deal_model.code
                err_msg = self.pm.deal_model.err_msg
            else:
                code = 9
                err_msg = ""
        table_name = "re_parse_ref"
        table_index = self.pm.task_setting.table_index
        stname = self.pm.task_setting.final_stat

        print(table_name, table_index, stname, self.pm.task_name,
              self.pm.task_tag, self.pm.id_dicts["rawid"], code,
              err_msg)

        await CoreSqlMixin.update_etl_ref_local_article(table_name, table_index, stname, self.pm.task_name,
                                                    self.pm.task_tag, self.pm.id_dicts["rawid"], code,
                                                    err_msg)
        return True, ""

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def deal_html_etl(self):
        self.step_info = self.add_all_process_info(f"处理解析，通过配置css和xpath进行解析")
        bools, err_info = await self.para_next()
        if not bools:
            return bools, err_info
        table_latest = json.loads(self.pm.task_setting.table_latest)
        mongo_conn_meta = Coll.get_table_conn(table_latest["meta"])  # 根据table名取得mongo链接
        meta_info = await NOSQLHelper.find_one(mongo_conn_meta, {"_id": self.pm.id_dicts["keyid"]})
        if not meta_info:
            return False,{"err_msg":"没有查询到该引文的题录数据"}
        self.para_dicts["meta_info"] = meta_info
        callmodel = EtlCallBackModel[
            self.sql_model_class,
            self.html_model_class](
            down_model=self.pm.down_model,
            para_dicts=self.para_dicts,
            sql_model=self.pm.sql_model,
            redis_all=self.redis_model
        )
        self.pm.deal_model = self.callback_deal_html_etl(callmodel)
        ref_state = self.pm.deal_model.ref_state
        if len(ref_state) > 0:
            mongo_conn = Coll.get_table_conn(table_latest["ref"])  # 根据table名取得mongo链接
            cjip = await NOSQLHelper.find_one(mongo_conn, {"_id": ref_state["lngid"]})
            if cjip:
                cjip["is_deprecated"] = "1"
                self.pm.down_model.save_data.append({"table": table_latest["ref"], "data": cjip})
                ref_depr_conn = Coll.get_table_conn("ref_deprecated")
                await NOSQLHelper.insert_one(ref_depr_conn, {"lngid": cjip["lngid"], "data": cjip})
        return True, {}

    def callback_deal_html_etl(self, callmodel) -> EtlDealModel:
        name = "_".join([self.pm.task_name, self.pm.task_tag, "ref","etl", "callback"])
        callback = eval(name)
        return callback(callmodel)
